const $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
/****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
const maxPos = $chars.length;
const randomString = (len) => {
    len = len || 32;
    let result = '', i = 0;
    for (; i < len; i++) {
        result += $chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return result;
};

/**
 * 数组去重
 */
const uniqueArray = (data) => {
    data = data || [];
    let arr = {};
    data.forEach((el, index) => {
        let v = data[index];
        if (typeof(arr[v]) == 'undefined') {
            arr[v] = 1;
        }
    });
    data.length = 0;
    for (let i in arr) {
        if (data.length < 10) {
            data[data.length] = i;
        }
    }
    return data;
};

const parseBr = (data) => {
    return data.replace(/\n/g, '<br />').replace(/\\n/g, '<br />');
};

/**
 * 获取url参数 @param string
 **/
const getQueryString = (name) => {
    const reg = new RegExp("(^|&|)" + name + "=([^&]*)(&|$)");
    const r = window.location.href.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
};

/**
 * @module compVer
 * @instance
 * @param {String} ver1 - 版本号1，格式 0.0.0
 * @param {String} ver2 - 版本号2，格式 0.0.0
 * @return {int} 1: ver1 > ver2, 0: ver1 = ver2, -1: ver1 < ver2
 * @desc 比较版本号
 */
const compVer = (ver1, ver2) => {
    var ver1periods = String(ver1).split('.'),
        ver2periods = String(ver2).split('.');
    var comp = function (a, b) {
        a = (a === undefined ? 0 : parseInt(a));
        b = (b === undefined ? 0 : parseInt(b));
        if (a > b) {
            return 1;
        }
        if (a == b) {
            return 0;
        }
        if (a < b) {
            return -1;
        }
    };

    var r0 = comp(ver1periods[0], ver2periods[0]),
        r1 = comp(ver1periods[1], ver2periods[1]),
        r2 = comp(ver1periods[2], ver2periods[2]);

    if (r0 == 0) {
        if (r1 == 0) {
            return r2;
        }
        return r1;
    }
    return r0;
};


/**
 * 对Date的扩展，将 Date 转化为指定格式的String
 * 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，
 * 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
 * 例子：
 * (new Date()).format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
 * (new Date()).format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
 * @param fmt
 * @returns {*}
 */
Date.prototype.format = function (fmt) { //author: meizz
    const o = {
        'M+': this.getMonth() + 1, //月份
        'd+': this.getDate(), //日
        'h+': this.getHours(), //小时
        'm+': this.getMinutes(), //分
        's+': this.getSeconds(), //秒
        'q+': Math.floor((this.getMonth() + 3) / 3), //季度
        'S': this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (let k in o) {
        if (new RegExp('(' + k + ')').test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(("" + o[k]).length)));
        }
    }
    return fmt;
};

Array.prototype.get = function (key, value, single = true) {
    const items = this.filter(el => {
        return el[key] == value;
    });
    if (items.length < 1) {
        return null;
    }
    return single ? items[0] : items;
};

Array.prototype.getIndex = function (key, value) {
    let index = -1;
    this.filter((el, i) => {
        if (!!key ? el[key] === value : el === value) {
            index = i;
        }
    });
    return index;
};

Array.prototype.arrayIndex = function (value) {
    let index = 0;
    this.filter((el, i) => {
        if(value > this[i] && value <= this[i+1]){
            index = i;
        }
    });
    return index;
};

const isArray = (arr) => {
    return Object.prototype.toString.call(arr) === '[object Array]';
};

export const util = {randomString, uniqueArray, parseBr, getQueryString, compVer, isArray};

